//
//  LGEditServiceInfoView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2025/6/24.
//

#import "LGEditServiceInfoView.h"
#import "DatePickerTwoView.h"
@interface LGEditServiceInfoView()<DatePickerViewDelegate,UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *startTitle;
/**   */
@property (nonatomic , strong)UIButton *startBtn;
/**   */
@property (nonatomic , strong)UILabel *endTitle;
/**   */
@property (nonatomic , strong)UIButton *endButton;
/**   */
@property (nonatomic , strong)UILabel *doctorTitle;
/**   */
@property (nonatomic , strong)UIButton *doctorBtn;
/**   */
@property (nonatomic , strong)UIButton *cancelBtn;
/**   */
@property (nonatomic , strong)UIButton *sureButton;
/**   */
@property (nonatomic , copy)NSString *currentDate;
/**   */
@property (nonatomic , copy)NSString *doctorid;

@end

@implementation LGEditServiceInfoView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
        CGFloat height = kMaxY(self.sureButton.frame)+viewPix(20);
        [UIView animateWithDuration:0.3 animations:^{
            self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.8];
            self.baseView.frame = CGRectMake(0, Screen_H-height, Screen_W, self.baseView.bounds.size.height);
        }];
    }
    return self;
}

-(void)setInfoDic:(NSDictionary *)infoDic{
    _infoDic = infoDic;
    self.doctorid = LGNSString(infoDic[@"doctor_id"]);
    NSString *startDate = LGNSString(infoDic[@"vip_date_start"]);
    NSString *endDate = LGNSString(infoDic[@"vip_date_end"]);
    NSString *doctor = LGNSString(infoDic[@"doctorname"]);
    [self.startBtn setTitle:startDate forState:UIControlStateSelected];
    [self.endButton setTitle:endDate forState:UIControlStateSelected];
    [self.doctorBtn setTitle:doctor forState:UIControlStateSelected];
    [self.startBtn setTitle:startDate forState:UIControlStateHighlighted];
    [self.endButton setTitle:endDate forState:UIControlStateHighlighted];
    [self.doctorBtn setTitle:doctor forState:UIControlStateHighlighted];
    self.startBtn.selected = [startDate isEmptyString]?NO:YES;
    self.endButton.selected = [startDate isEmptyString]?NO:YES;
    self.doctorBtn.selected = [startDate isEmptyString]?NO:YES;
}


//选择时间
-(void)selectDateAction:(UIButton *)sender{
    NSString *selectDate = LGNSString([sender titleForState:UIControlStateSelected]);
    self.currentDate = (sender == self.startBtn)?@"start":@"end";
    DatePickerTwoView *pickerView = [[DatePickerTwoView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.title = (sender == self.startBtn)?@"开始时间":@"结束时间";
    pickerView.currentDate = [selectDate isEmptyString]?@"":selectDate;
    pickerView.delegate = self;
    [LGKeyWindow addSubview:pickerView];
}

-(void)sendSelectDate:(NSString *)date index:(NSInteger)index{
    if ([self.currentDate isEqualToString:@"start"]) {
        //开始时间
        [self.startBtn setTitle:date forState:UIControlStateSelected];
        [self.startBtn setTitle:date forState:UIControlStateHighlighted];
        self.startBtn.selected = YES;
        
    }else if ([self.currentDate isEqualToString:@"end"]){
        //结束时间
        [self.endButton setTitle:date forState:UIControlStateSelected];
        [self.endButton setTitle:date forState:UIControlStateHighlighted];
        self.endButton.selected = YES;
    }
}


//选择医生
-(void)selectDoctorAction{
    [UIView animateWithDuration:0.3 animations:^{
        self.tableView.frame = CGRectMake(0, viewPix(110), Screen_W, Screen_H-viewPix(100));
        [self.tableView reloadData];
    }];
}

-(void)cancelButtonAction{
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, self.baseView.bounds.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)sureBtnAction{
    NSString *startDate = [self.startBtn titleForState:UIControlStateSelected];
    NSString *endDate = [self.endButton titleForState:UIControlStateSelected];
    NSString *doctor = [self.doctorBtn titleForState:UIControlStateSelected];
    if ([startDate isEmptyString]) {
        [TooltipView showMessage:@"请选择开始时间" offset:0];
        return;
    }
    if ([endDate isEmptyString]) {
        [TooltipView showMessage:@"请选择结束时间" offset:0];
        return;
    }
    if ([doctor isEmptyString]) {
        [TooltipView showMessage:@"请选择医生" offset:0];
        return;
    }
    NSDictionary *saveDic = @{@"vip_date_start":startDate , @"vip_date_end":endDate , @"vip_id":LGNSString(self.infoDic[@"vip_record_id"]), @"system_user_id":self.doctorid};
    if (self.delegate && [self.delegate respondsToSelector:@selector(saveServiceInfo:)]) {
        [self.delegate saveServiceInfo:saveDic];
    }
    [self cancelButtonAction];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.doctorArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = LGNSString(self.doctorArray[indexPath.row][@"title"]);
    cell.textLabel.textColor = [UIColor colorWithString:@"#333333"];
    cell.textLabel.numberOfLines = 2;
    cell.textLabel.font = LGFont(16);
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.doctorid = LGNSString(self.doctorArray[indexPath.row][@"value"]);
    [self.doctorBtn setTitle:LGNSString(self.doctorArray[indexPath.row][@"title"]) forState:UIControlStateSelected];
    [self.doctorBtn setTitle:LGNSString(self.doctorArray[indexPath.row][@"title"]) forState:UIControlStateHighlighted];
    self.doctorBtn.selected = YES;
    [UIView animateWithDuration:0.3 animations:^{
        self.tableView.frame = CGRectMake(0, Screen_H, Screen_W, Screen_H-viewPix(100));
    }];
}

-(void)closeDoctorView{
    [UIView animateWithDuration:0.3 animations:^{
        self.tableView.frame = CGRectMake(0, Screen_H, Screen_W, Screen_H-viewPix(100));
    }];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.startTitle];
    [self.baseView addSubview:self.startBtn];
    [self.baseView addSubview:self.endTitle];
    [self.baseView addSubview:self.endButton];
    [self.baseView addSubview:self.doctorTitle];
    [self.baseView addSubview:self.doctorBtn];
    [self.baseView addSubview:self.cancelBtn];
    [self.baseView addSubview:self.sureButton];
    [self addSubview:self.tableView];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]initWithFrame:CGRectMake(0, Screen_H, Screen_W, Screen_H)];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"编辑服务包" colorString:@"#333333" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(0, viewPix(15), Screen_W, viewPix(22));
    }
    return _titleLabel;
}

-(UILabel *)startTitle{
    if(!_startTitle){
        _startTitle = [UILabel lableWithText:@"开始时间" colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
        _startTitle.frame = CGRectMake(viewPix(20), kMaxY(self.titleLabel.frame)+viewPix(15), viewPix(100), viewPix(40));
    }
    return _startTitle;
}

-(UIButton *)startBtn{
    if(!_startBtn){
        _startBtn = [UIButton buttonWithTitle:@"请选择" titleFont:LGFont(15) normalColor:@"#999999" selectColor:@"#333333"];
        _startBtn.frame = CGRectMake(viewPix(130), kMinY(self.startTitle.frame), Screen_W-viewPix(150), viewPix(40));
        [_startBtn setTitleColor:[UIColor colorWithString:@"#333333"] forState:UIControlStateHighlighted];
        [_startBtn addBorder:@"#F2F2F2" lineWidth:1 cornerRidus:5];
        _startBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _startBtn.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(10), 0, 0);
        [_startBtn addTarget:self action:@selector(selectDateAction:) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
        tipView.frame = CGRectMake(Screen_W-viewPix(150)-26, (viewPix(40)-16)/2.0, 16, 16);
        [_startBtn addSubview:tipView];
    }
    return _startBtn;
}

-(UILabel *)endTitle{
    if(!_endTitle){
        _endTitle = [UILabel lableWithText:@"结束时间" colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
        _endTitle.frame = CGRectMake(viewPix(20), kMaxY(self.startTitle.frame)+viewPix(10), viewPix(100), viewPix(40));
    }
    return _endTitle;
}

-(UIButton *)endButton{
    if(!_endButton){
        _endButton = [UIButton buttonWithTitle:@"请选择" titleFont:LGFont(15) normalColor:@"#999999" selectColor:@"#333333"];
        _endButton.frame = CGRectMake(viewPix(130), kMinY(self.endTitle.frame), Screen_W-viewPix(150), viewPix(40));
        [_endButton setTitleColor:[UIColor colorWithString:@"#333333"] forState:UIControlStateHighlighted];
        [_endButton addBorder:@"#F2F2F2" lineWidth:1 cornerRidus:5];
        _endButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _endButton.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(10), 0, 0);
        [_endButton addTarget:self action:@selector(selectDateAction:) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
        tipView.frame = CGRectMake(Screen_W-viewPix(150)-26, (viewPix(40)-16)/2.0, 16, 16);
        [_endButton addSubview:tipView];
    }
    return _endButton;
}

-(UILabel *)doctorTitle{
    if(!_doctorTitle){
        _doctorTitle = [UILabel lableWithText:@"医生" colorString:@"#333333" textFont:LGFont(15) textAlignment:NSTextAlignmentLeft lines:1];
        _doctorTitle.frame = CGRectMake(viewPix(20), kMaxY(self.endTitle.frame)+viewPix(10), viewPix(100), viewPix(40));
    }
    return _doctorTitle;
}

-(UIButton *)doctorBtn{
    if(!_doctorBtn){
        _doctorBtn = [UIButton buttonWithTitle:@"请选择" titleFont:LGFont(15) normalColor:@"#999999" selectColor:@"#333333"];
        _doctorBtn.frame = CGRectMake(viewPix(130), kMinY(self.doctorTitle.frame), Screen_W-viewPix(150), viewPix(40));
        [_doctorBtn setTitleColor:[UIColor colorWithString:@"#333333"] forState:UIControlStateHighlighted];
        [_doctorBtn addBorder:@"#F2F2F2" lineWidth:1 cornerRidus:5];
        _doctorBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _doctorBtn.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(10), 0, viewPix(17));
        _doctorBtn.titleLabel.numberOfLines = 2;
        [_doctorBtn addTarget:self action:@selector(selectDoctorAction) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
        tipView.frame = CGRectMake(Screen_W-viewPix(150)-26, (viewPix(40)-16)/2.0, 16, 16);
        [_doctorBtn addSubview:tipView];
    }
    return _doctorBtn;
}

-(UIButton *)cancelBtn{
    if(!_cancelBtn){
        _cancelBtn = [UIButton buttonWithTitle:@"取消" titleFont:LGFontWeight(16, UIFontWeightMedium) normalColor:@"#429BFF" selectColor:@"#429BFF" frame:CGRectMake(viewPix(20), kMaxY(self.doctorBtn.frame)+viewPix(25), (Screen_W-viewPix(60))/2.0, viewPix(40))];
        [_cancelBtn addBorder:@"#429BFF" lineWidth:1 cornerRidus:5];
        [_cancelBtn addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

-(UIButton *)sureButton{
    if(!_sureButton){
        _sureButton = [UIButton buttonWithBackColor:@"#429BFF" cornerRadius:5 title:@"确定" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#FFFFFF" frame:CGRectMake(kMaxX(self.cancelBtn.frame)+viewPix(20), kMinY(self.cancelBtn.frame), (Screen_W-viewPix(60))/2.0, viewPix(40))];
        [_sureButton addTarget:self action:@selector(sureBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureButton;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, Screen_H, Screen_W, Screen_H-viewPix(100)) style:UITableViewStylePlain];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = viewPix(40);
        _tableView.cornerRidus = 10;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [self tableHeaderView];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(30))];
    }
    return _tableView;
}

-(UIView *)tableHeaderView{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(30))];
    UIButton *closeBtn = [UIButton buttonWithImage:@"closeGray16" frame:CGRectMake(Screen_W-viewPix(35), viewPix(5), viewPix(30), viewPix(30))];
    [closeBtn addTarget:self action:@selector(closeDoctorView) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:closeBtn];
    return headView;
}

@end
